home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / METAKIT.ZIP / EXAMPLES / CATRECV / MYDLG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-09  |  3.1 KB  |  135 lines

  1. //    mydlg.cpp  -  display server sample code
  2. //
  3. //    This is a part of the MetaKit library.
  4. //    Copyright (c) 1996 Meta Four Software.
  5. //    All rights reserved.
  6. /////////////////////////////////////////////////////////////////////////////
  7.  
  8. #include "stdafx.h"
  9. #include "catrecv.h"
  10. #include "MyDlg.h"
  11. #include "TreeDlg.h"
  12.  
  13. #ifdef _DEBUG
  14. //#define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CMyDlg dialog
  21.  
  22. CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
  23.     : CDialog(CMyDlg::IDD, pParent), _timer (0), _server (0)
  24. {
  25.     //{{AFX_DATA_INIT(CMyDlg)
  26.     //}}AFX_DATA_INIT
  27. }
  28.  
  29. CMyDlg::~CMyDlg ()
  30. {
  31.     delete _server;                        // shutdown the server
  32. }
  33.  
  34. void CMyDlg::DoDataExchange(CDataExchange* pDX)
  35. {
  36.     CDialog::DoDataExchange(pDX);
  37.     //{{AFX_DATA_MAP(CMyDlg)
  38.     DDX_Control(pDX, IDC_PORT, m_port);
  39.     //}}AFX_DATA_MAP
  40. }
  41.  
  42. BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
  43.     //{{AFX_MSG_MAP(CMyDlg)
  44.     ON_EN_CHANGE(IDC_PORT, OnChangePort)
  45.     ON_WM_TIMER()
  46.     //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48.  
  49. /////////////////////////////////////////////////////////////////////////////
  50. // [JCW]  This code added for MetaKit CATRECV
  51.  
  52. BOOL CMyDlg::OnInitDialog()
  53. {
  54.     CDialog::OnInitDialog();
  55.  
  56.     m_port.SetWindowText("12345");
  57.  
  58.     return TRUE;  // return TRUE  unless you set the focus to a control
  59. }
  60.  
  61. // called at start and whenever the m_port edit control has changed
  62. void CMyDlg::OnChangePort() 
  63. {
  64.     delete _server;
  65.     _server = 0;
  66.  
  67.     KillTimer(_timer);                    // forget pending timeouts
  68.     _timer = SetTimer(1, 1000, 0);        // timeout 1 second from now
  69. }
  70.  
  71.     // this will be called by the CServer object to set up a new connection 
  72.     static void StartNewConnection(CFile& file_)
  73.     {
  74.         DEBUG_NEW CTreeDialog (file_);    // creates a modeless dialog box
  75.     }
  76.  
  77. // use a delay to wait for a good port number before (re-)creating a server
  78. void CMyDlg::OnTimer(UINT nIDEvent) 
  79. {
  80.     KillTimer(_timer);                    // avoid further timeouts
  81.     _timer = 0;
  82.  
  83.         // called one second after the last change to the m_port edit control
  84.     CString s;
  85.     m_port.GetWindowText(s);
  86.     int port = atoi(s);
  87.  
  88.     if (port > 0)
  89.     {
  90.             // set up a new server listening on the specified port
  91.         ASSERT(!_server);
  92.         _server = DEBUG_NEW CServer (port, StartNewConnection);
  93.     }
  94.     else
  95.         MessageBeep(MB_OK);
  96.  
  97.     CDialog::OnTimer(nIDEvent);
  98. }
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CServer socket, this sample code does no error checking
  102.  
  103. CServer::CServer (int port_, ConnectHandler handler_)
  104.     : _port (port_), _handler (handler_)
  105. {
  106.     OnClose(0);    // simulate a lost connection to start things up
  107. }
  108.  
  109. CServer::~CServer ()
  110. {
  111.     Close();
  112. }
  113.  
  114. void CServer::OnAccept(int /* error_ */)
  115. {
  116.     CSocket session;
  117.     VERIFY(Accept(session));
  118.  
  119.     CSocketFile data (&session);
  120.  
  121.     ASSERT(_handler);
  122.     _handler(data);
  123. }
  124.  
  125. void CServer::OnClose(int /* error_ */)
  126. {
  127.     Close();
  128.  
  129.     VERIFY(Create(_port));
  130.     VERIFY(Listen());
  131. }
  132.  
  133. /////////////////////////////////////////////////////////////////////////////
  134. // $Id: mydlg.cpp,v 1.2 1996/12/04 14:49:47 jcw Exp $
  135.